<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
    </template>
  </list_url>
</template>
<script>
export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    if (is_del){
      if(in_right_admin('Param/restore')) btns['恢复'] = function(){ app.$refs['list_url'].restore() }
      if(in_right_admin('Param/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }
    }else {
      if(in_right_admin('Param/add')) btns['添加'] = function(){ app.iframe('edit') }
      if(in_right_admin('Param/del')) btns['删除'] = function(){ app.$refs['list_url'].del() }
      if(in_right_admin('Param/recycle')) btns['回收站'] = function(){ app.iframe('index?is_del=1') }
    }
    let type_map = this.field_type(this.field_type.CAN_PARAM, 'label');
    let validate_map = {'["nullable"]':'否','["required"]':'是'};
    let forms_config = {
       label: {label:'参数名',name:'where[name][like]',type:'select_one',config:[],value:''},
    };
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', type: 'hidden', order:true},
      name:{name:'name', label:'参数名', width: '120px'},
      type:{name:'type', label:'输入类型', width: '120px', type: 'select_one', config: type_map},
      tips:{name:'tips', label:'提示信息'},
      validate: {name:'validate',label:'验证规则',type:'validate_rule',value:'[]', min_width: '200px'},
      value:{name:'value', label:'默认值', width: '120px'},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    if (!this.admin_config('this_tenant.id')){
      list_cols['tenant_id'] = {name:'tenant_id', label:'租户', width:'65px', fixed:'right'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'80px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/param/'+(is_del?'recycle':'list'),
        title: "自定义参数列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
    }
  },
  methods: {
    to_edit(row){
      let can_edit = in_right_admin('Param/edit') && ~[0,row.tenant_id].indexOf(this.admin_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      this.iframe('edit?'+s94Mjs.param_encode(param))
    },
    edit(row,col,v){
      let app = this;
      if(!in_right_admin('Param/edit')) return;
      ajax(`/admin/param/edit?id=${row.id}&${col.name}=${v}`, function (res){
        if (col.type=='radio_switch' && res.data==0) app.$refs['list_url'].list();
        if (col.type!='radio_switch' && res.data>0) app.$refs['list_url'].list();
      })
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
  }
}
</script>
